package com.yy.controller.mapper;

import com.github.pagehelper.Page;
import com.yy.dto.OrderPageQueryDTO;
import com.yy.entity.Orders;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.time.LocalDateTime;
import java.util.List;

@Mapper
public interface OrderMapper {
    /**
     * 条件分页查询订单
     *
     * @param orderPageQueryDTO
     * @return
     */
    Page<Orders> list(OrderPageQueryDTO orderPageQueryDTO);

    /**
     * 获取所有订单
     *
     * @return
     */
    @Select("select * from orders")
    List<Orders> AllOrder();

    /**
     * 不为取消,订单状态值+1(若订单状态为4,deliveryTime不为空,添加送达时间)
     *
     * @param id
     * @param deliveryTime
     */
    @Update("update orders set status = status+1,delivery_time=#{deliveryTime} " +
            "where id = #{id}")
    void upDateStatus(Long id, LocalDateTime deliveryTime);

    /**
     * 取消,修改订单状态,添加取消原因
     *
     * @param id
     * @param cancelReason
     */
    @Update("update orders set status =6,cancel_reason = #{cancelReason}" +
            ",cancel_time=#{cancelTime} where id = #{id}")
    void upDateStatusAndReason(Long id, String cancelReason, LocalDateTime cancelTime);

    /**
     * 根据id获取订单状态
     *
     * @param id
     * @return
     */
    @Select("select status from orders where id = #{id}")
    Integer statusById(Long id);

    /**
     * 添加订单
     *
     * @param orders
     */
    void add(Orders orders);

    /**
     * 通过id查询订单
     *
     * @param id
     * @return
     */
    @Select("select * from orders where id=#{id}")
    Orders getByid(Long id);

    /**
     * 获取用户订单
     *
     * @param userId
     * @return
     */
    @Select("select * from orders where user_id = #{userId} order by order_time desc")
    List<Orders> getListByUserId(Long userId);

    /**
     * 支付成功
     *
     * @param orders
     */
    @Update("update orders set status =#{status},pay_method=#{payMethod}," +
            "checkout_time=#{checkoutTime} where id = #{id}")
    void paySucess(Orders orders);

    /**
     * 用户取消订单
     *
     * @param id
     * @param cancelReason
     */
    @Update("update orders set status = 6,cancel_reason = #{cancelReason}," +
            " cancel_time=#{cancelTime} where id = #{id} and user_id=#{userId}")
    void userCancel(Long id, String cancelReason, LocalDateTime cancelTime, Long userId);
}
